/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-06     misonyo   first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

/* defined the LED0 pin: PD14 */
#define LED0_PIN    GET_PIN(D, 14)
#define LED1_PIN    GET_PIN(D, 15)

uint8_t led1 = 0;
void thread1_signal_handler(int sig)
{
    led1 = 1;
}

void thread_entry(void *parameter)
{
    /* set LED0 pin mode to output */
    rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
    
    /* 安装信号 */
    rt_signal_install(SIGUSR1, thread1_signal_handler);
    rt_signal_unmask(SIGUSR1);

    rt_pin_write(LED1_PIN, PIN_LOW);
    while(1)
    {
        rt_pin_write(LED1_PIN, led1);
        rt_thread_delay(100);
    }
}

int main(void)
{
    rt_thread_t tid =  rt_thread_create("thread", thread_entry, RT_NULL, 1024, (RT_THREAD_PRIORITY_MAX / 3) + 1 , 1);
    if(tid)
    {
        rt_thread_startup(tid);
    }

    /* set LED0 pin mode to output */
    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);

    rt_pin_write(LED0_PIN, PIN_LOW);

    rt_thread_delay(1000);

    /* 发送信号 SIGUSR1 给线程 1 */
    rt_thread_kill(tid, SIGUSR1);

    while (1)
    {
        rt_pin_write(LED0_PIN, PIN_HIGH);
        rt_thread_delay(100);
    }

    return RT_EOK;
}
